package com.woniu.darryringmall.admin.config;

import com.woniu.darryringmall.admin.jwt.JwtFilter;
import com.woniu.darryringmall.admin.jwt.JwtRealm;
import com.woniu.darryringmall.admin.shiro.DbRealm;
import com.woniu.darryringmall.admin.shiro.MyPerms;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public DbRealm realm() {
        DbRealm dbRealm = new DbRealm();
        dbRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return dbRealm;
    }

    @Bean
    public Realm jwtRealm() {
        JwtRealm jwtRealm = new JwtRealm();
        return jwtRealm;
    }

    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置加密方式和次数
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        hashedCredentialsMatcher.setHashIterations(1024);
        return hashedCredentialsMatcher;
    }

    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        MultiRealmAuthenticator authenticator = new MultiRealmAuthenticator();
        authenticator.setRealms(Arrays.asList(realm(), jwtRealm()));
        securityManager.setAuthenticator(authenticator);
        securityManager.setAuthorizer(realm());
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager());
//        //未认证的情况下
//        factoryBean.setLoginUrl("/admin/manager/login");
//        //未授权的情况下
//        factoryBean.setUnauthorizedUrl("/admin/manager/login");
        Map<String, Filter> filters = new HashMap<>();
        filters.put("myPerms", new MyPerms());
        filters.put("jwt", new JwtFilter());
        factoryBean.setFilters(filters);
        try {
            factoryBean.setFilterChainDefinitionMap(permMapFactoryBean().getObject());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return factoryBean;
    }

    @Bean
    public PermMapFactoryBean permMapFactoryBean() {
        PermMapFactoryBean permMapFactoryBean = new PermMapFactoryBean();
        return permMapFactoryBean;
    }

//    @Bean
//    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
//        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
//        chainDefinition.addPathDefinition("/manager/login", "anon");
//        chainDefinition.addPathDefinition("/menu/get", "authc");
//        chainDefinition.addPathDefinition("/logout", "logout");
//        chainDefinition.addPathDefinition("/subject/add" , "perms[AdminManager]");
//        return chainDefinition;
//    }
}
